﻿@inherits AppPage

<div style="padding:50px 30px;text-align:center;">
    <Avatar Size="5rem" Src="@user?.AvatarUrl" />
    <div style="margin-top:10px;">@user?.Name (@user?.UserName)</div>
</div>
<div style="padding:20px;">
    <AntList DataSource="items">
        <ListItem OnClick="()=>OnItemClick(context)">
            <div>
                <Icon Type="@context.Icon" />
                <span>@context.Name</span>
            </div>
            @if (context.ComType != null)
            {
                <Icon Type="right" />
            }
        </ListItem>
    </AntList>
</div>
<div style="padding:20px;">
    <Button Type="@ButtonType.Primary" Danger Block OnClick="e=>Logout()">@Language["Nav.Exit"]</Button>
</div>

@code {
    private UserInfo user;
    private List<Known.MenuItem> items = [];

    protected override async Task OnInitAsync()
    {
        await base.OnInitAsync();
        user = CurrentUser;
        items.Add(new Known.MenuItem { Id = "Mobile", Name = user?.Mobile, Icon = "mobile" });
        items.Add(new Known.MenuItem { Id = "Email", Name = user?.Email, Icon = "inbox" });
        items.Add(new Known.MenuItem
        {
            Id = "Profile",
            Name = Language.GetString("Title.MyProfile"),
            Icon = "user",
            ComType = typeof(UserEditForm)
        });
        items.Add(new Known.MenuItem
        {
            Id = "Password",
            Name = Language.GetString("Title.SecuritySetting"),
            Icon = "lock",
            ComType = typeof(PasswordEditForm)
        });
    }

    private void OnItemClick(Known.MenuItem item) => App.Navigate(item);

    private void Logout()
    {
        UI.Confirm(Language["Tip.Exits"], async () =>
        {
            var user = CurrentUser;
            var result = await Platform.SignOutAsync(user?.Token);
            if (result.IsValid)
            {
                await App.LogoutAsync();
                Config.OnExit?.Invoke();
            }
        });
    }
}